<template>
  <div class="service-block">
    <div class="grow-wrapper">
      <div class="content-center">
        <div style="margin: 0 0 10px 0">
          <a-form
            :model="pageInfo"
            name="horizontal_login"
            layout="inline"
            autocomplete="off"
          >
            <a-form-item label="课程名称">
              <a-input v-model:value="pageInfo.name" placeholder="课程名称" />
            </a-form-item>
            <a-form-item label="任务名称">
              <a-input v-model:value="pageInfo.taskName" placeholder="任务名称" />
            </a-form-item>
            <a-form-item>
              <a-space size="large">
                <a-button type="primary" @click="onSearch" :icon="h(SearchOutlined)">查询</a-button>
              </a-space>
            </a-form-item>
          </a-form>
        </div>
        <a-spin :spinning="loading">
          <template v-if="userCourseItems?.length > 0">
            <a-table
              :columns="columns"
              :data-source="userCourseItems"
              :loading="loading"
              :pagination="paginationTable"
              @change="handleTableChange"
              bordered
            >
            </a-table>
          </template>
          <EmptyData v-else />
        </a-spin>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { h, onMounted, reactive, ref } from "vue";
import { apiUserCourseCatalogList } from "@/api/course";
import { useRouter } from "vue-router";
import { SearchOutlined } from "@ant-design/icons-vue";
import toFixed from "xe-utils/toFixed";
import EmptyData from "@/components/public/EmptyData.vue";

const router = useRouter();

const userCourseItems = ref<any>([]);
const total = ref<number>(0);
const loading = ref<boolean>(false);

const paginationTable = reactive({
  total: 0,
  current: 1,
  pageSize: 10,//每页中显示10条数据
  showTotal: total => `共有 ${total} 条数据`  //分页中显示总的数据
});

const columns = [
  {
    title: "课程名称",
    dataIndex: "courseName",
    width: "20%"
  },
  {
    title: "任务名称",
    dataIndex: "taskName",
    width: "15%"
  },
  {
    title: "已学时长",
    dataIndex: "totalLearnTime",
    width: "15%",
    customRender: ({ text }) => {
      if (text && text / 60 > 1) {
        return toFixed(text / 60, 0) + "分钟";
      } else {
        return text + "秒";
      }
    }
  },
  {
    title: "学习进度",
    dataIndex: "learnProcess",
    width: "15%",
    customRender: ({ text }) => {
      if (text && text >= 1) {
        return "已完成";
      } else {
        return (text * 100).toFixed(0) + "%";
      }
    }
  },
  {
    title: "最近学习",
    dataIndex: "lastLearnTime",
    width: "20%"
  }
];

async function handleTableChange(pagination) {
  paginationTable.current = pagination.current;
  paginationTable.pageSize = pagination.pageSize;
  pageInfo.value.pageNo = pagination.current;
  pageInfo.value.pageSize = pagination.pageSize;
  await getUserCourseCatalogList();
}

const pageInfo = ref<{
  pageNo: number;
  pageSize: number;
  name: string;
  taskName: string;
}>({
  // 当前页码
  pageNo: 1,
  // 当前每页显示多少条数据
  pageSize: 10,
  name: "",
  taskName: ""
});

async function getUserCourseCatalogList() {
  loading.value = true;
  userCourseItems.value = [];
  const data: any = await apiUserCourseCatalogList(pageInfo.value);
  userCourseItems.value = data.result?.records;
  total.value = data.result?.total;
  loading.value = false;
}

function onSearch() {
  pageInfo.value.pageNo = 1;
  getUserCourseCatalogList();
}

onMounted(() => {
  getUserCourseCatalogList();
});

</script>
<style lang="less" scoped>
.service-block {
  width: 100%;
  height: 87.6vh;
}

.grow-wrapper {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: space-between;
}

.content-center {
  margin: 0 10px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 100%;
}

:deep(.ant-card-body) {
  padding: 10px;
}

a {
  color: black;
}

a:hover {
  color: #3370ff;
}

</style>
